/*
 *  WsvJdbc - a JDBC driver for WSV (values separated by whatever) data files
 *  Copyright (C) 2004  Daniele Pes
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package axs.jdbc.dataSourceConfiguration;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Properties;


/**
 * This class is meant to hide the technology used to gather import specifications from file (for the WsvJdbc driver).
 * At the moment a file of properties is used.
 * 
 * @author 		Daniele Pes
 * @author 		Marco Pozzato
 * @version    $Id: JdbcSpecificationProperties.java,v 1.2 2007/04/16 17:24:36 montag451 Exp $
 */

public class JdbcSpecificationProperties {
	
	private static final String DEFAULT_PROPERTIES_FORMAT = "ISO-8859-1";
    public static final String DEFAULT_PROPERTIES_FILE_NAME = "schema.ini";
	
	private Properties props;		
	
	public JdbcSpecificationProperties(){}

	public JdbcSpecificationProperties(String fileName)
	throws SpecificationException{
		if(fileName.equals(""))
			fileName = DEFAULT_PROPERTIES_FILE_NAME;
		try{
			getPropertiesFromFile(fileName);
		}
		catch(SpecificationException e){
			throw new SpecificationException("Unable to get import properties from specification file '" + fileName 
														+ "' \n-> (SpecificationException) " + e.getMessage(), false);
		}		
	}
	
		
	// gets properties from specification file
	public void getPropertiesFromFile(String fileName)
	throws SpecificationException{		      
		try{
			// during initialization source settings are got from import specification file				
			Properties props = new Properties();
			FileInputStream in = new FileInputStream(fileName);
			// Please remember that the deafult constructor of class Properties assumes that the file
			// it gathers properties from (through load method) is encoded according to using ISO 8859-1 (Latin-1)
			// so some chars may not be correctly recognized if they are written, for example, using UTF-8 
			// Take a look at http://www.google.it/search?q=cache:did1aMxR_A4J:java.sun.com/developer/JDCTechTips/2003/tt0506.html+file+utf-8+latin1+%C2%A7+issue+property+java&hl=it*/
			// for same useful explaination
			props.load(in);
			in.close();
			setProperties(props);
		}
		catch(IOException e){			
			throw new SpecificationException("Unable to open a stream on the file (" + fileName + ") of source specifications \n-> (IOException) " + e.getMessage());
		}					 
	}

	public void getProperties(String fileName, String charSetName)
	throws SpecificationException{
	    if(charSetName == null || charSetName.equals(""))
	        throw new SpecificationException("Invalid charset name was specified for properies having to be gathered from file.\n" +
	        		"Good ones (e.g.) are: \n" +
	        		"US-ASCII\n" +
	        		"ISO-8859-1\n" +
	        		"UTF-8\n" +
	        		"UTF-16BE\n" +
	        		"UTF-16LE\n" +
	        		"UTF-16");
	    try {	       
	        FileInputStream fileis = new FileInputStream(fileName);
           BufferedReader isr = new BufferedReader(new InputStreamReader(fileis, charSetName));	        
           String latinPropFileName = fileName + "." + DEFAULT_PROPERTIES_FORMAT; // e.g.: schema.ini.8859_1
           deleteFile(latinPropFileName);
           FileOutputStream fileos = new FileOutputStream(latinPropFileName);
           BufferedWriter obufferw = new BufferedWriter(new OutputStreamWriter(fileos, DEFAULT_PROPERTIES_FORMAT));
           String line;           
           while((line = isr.readLine()) != null){               
               obufferw.write(line + "\r\n");
           }
           obufferw.close();
           fileis.close();
           fileos.close();
           getPropertiesFromFile(latinPropFileName);
           deleteFile(latinPropFileName);
       } catch (Exception e) {
           throw new SpecificationException("Unable to open a stream on the file (" + fileName + ") of source specifications using " + charSetName + " as encoding\n-> (IOException) " + e.getMessage());
       } 
	}
	
	
	/**
     * @param latinPropFileName
     */
    private void deleteFile(String latinPropFileName) {
        File f = new File(latinPropFileName);
           if(f.exists())
               f.delete();
    }

    /**
	 * The method returns  <code>null</code> if the property is not found.
	 * 
	 * @param  name name of property
	 * @return the value of property named by 'name' param
	 * @throws SpecificationException
	 */
	public String getProperty(String name)
	throws SpecificationException{
	   Properties props = getProperties();
		if (props != null)
			return props.getProperty(name);
		else
			throw new SpecificationException("It's not been defined the property object having name -> " + name); 
	}

	
	public Properties getProperties(){
		return props;
	}	
	
	public void setProperties(Properties p){
		props = p;
	}
}